﻿using System;
using System.Windows.Forms;
using FastFood.Business.Entities;
using FastFood.Business.Logic;
using FastFood.Windows.Helpers;


namespace FastFood.Windows.Forms.Procesos
{
    //public delegate void GetObjeto(object o );

    public partial class CwfClienteBusqueda : UserControl
    {
        InsumosManager insumoManager;
        public event GetObjeto CwfProveedorBusqueda_SeleccionEntidad;

        public CwfClienteBusqueda()
        {
            InitializeComponent();
            insumoManager = new InsumosManager();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            buscarCliente();
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            limpiar();
        }

        private void insumosDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                if (CwfProveedorBusqueda_SeleccionEntidad != null && clientesBindingSource.Current != null)
                    CwfProveedorBusqueda_SeleccionEntidad(clientesBindingSource.Current);
            }

        }

        private void buscarCliente()
        {
            if (this.cmbTipoBusqueda.SelectedIndex != -1)
            {
                try
                {
                    switch (this.cmbTipoBusqueda.SelectedIndex)
                    {
                        case 0:
                            this.clientesBindingSource.DataSource = new ClientesManager().ListarByRUC(this.txtBuscar.Text);
                            break;
                        case 1:
                            this.clientesBindingSource.DataSource = new ClientesManager().ListarByDNI(this.txtBuscar.Text);
                            break;
                        case 2:
                            this.clientesBindingSource.DataSource = new ClientesManager().ListarByApellidos(this.txtBuscar.Text);
                            break;
                        case 3:
                            this.clientesBindingSource.DataSource = new ClientesManager().ListarByNombres(this.txtBuscar.Text);
                            break;
                        default:
                            return;
                    }
                }
                catch (Exception ex)
                {
                    Mensaje.ShowMessageAlert(this.ParentForm, Constantes.Titulo, ex.Message);
                }
            }
        }

        public void limpiar()
        {
            txtBuscar.Text = string.Empty;
            cmbTipoBusqueda.SelectedIndex = 0;
            clientesBindingSource.Clear();
            txtBuscar.Focus();
        }

        private void txtNombre_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (clientesBindingSource.Count > 0)
                {
                    clientesDataGridView.Focus();
                    clientesBindingSource.MoveFirst();
                }
            }

            else if (e.KeyCode == Keys.Enter)
            {
                if (clientesBindingSource.Count == 1)
                {
                    CwfProveedorBusqueda_SeleccionEntidad(clientesBindingSource.Current);
                }
            }
        }

        private void CwfInsumoBusqueda_Load(object sender, EventArgs e)
        {
            cmbTipoBusqueda.Focus();
        }

        private void insumosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (CwfProveedorBusqueda_SeleccionEntidad != null && clientesBindingSource.Current != null)
                CwfProveedorBusqueda_SeleccionEntidad(clientesBindingSource.Current);
        }

        private void txtBuscar_TextChanged(object sender, EventArgs e)
        {
            buscarCliente();
        }
    }
}
